package com.daykw.ip.task.thread;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;

import com.daykw.ip.model.Ips;

public class CheckProxyIpThread extends Thread {

	private Ips ips;

	private Dao dao;

	public CheckProxyIpThread(Ips ips, Dao dao) {
		this.ips = ips;
		this.dao = dao;
	}

	@Override
	public void run() {

		String reqUrl = "https://www.baidu.com/";
		String proxyHost = ips.getIps().split(":")[0];
		Integer proxyPort = Integer.parseInt(ips.getIps().split(":")[1]);
		HttpMethod method = new GetMethod(reqUrl);
		try {
			HttpClient httpClient = new HttpClient();
			httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);

			// 连接超时时间（默认10秒 10000ms） 单位毫秒（ms）
			int connectionTimeout = 5000;
			// 读取数据超时时间（默认30秒 30000ms） 单位毫秒（ms）
			int soTimeout = 5000;
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
			httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);
			int statusCode = httpClient.executeMethod(method);
			if (statusCode == 200) {
				// System.out.format("%s:%s-->%s\n", proxyHost, proxyPort,
				// statusCode);
				dao.clear(Ips.class, Cnd.where("id", "=", ips.getId()));
				//System.out.println("########删除:" + ips.getIps());

			}
		} catch (Exception e) {
			// e.printStackTrace();
		} finally {
			method.abort();
			method.releaseConnection();
		}

	}

}
